﻿// ===============================================================================
// XCI.Parent 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using System;
using System.Windows.Forms;
using XCI.Sys.Model;
using XCI.Sys.Service;
using XCI.Extensions;
using XCI.Windows;
using XCI.Windows.Extensions;
using XCI.Windows.Forms;
using XCI.Windows.Helper;

namespace XCI.Sys.Forms
{
    /// <summary>
    /// 系统字典管理
    /// </summary>
    [FormCaption("系统字典管理")]
    public partial class FrmDicManager : FrmBase
    {
        /// <summary>
        /// 表格操作类
        /// </summary>
        private GridHelper<SysDicCategory> helper;
        private readonly bool allowSelect = SysRuntime.IsAuthorize("sys.dicCategory.select");
        private readonly bool allowInsert = SysRuntime.IsAuthorize("sys.dicCategory.insert");
        private readonly bool allowUpdate = SysRuntime.IsAuthorize("sys.dicCategory.update");
        private readonly bool allowDelete = SysRuntime.IsAuthorize("sys.dicCategory.delete");

        public FrmDicManager()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 页面初始化
        /// </summary>
        private void InitForm()
        {
            //1.初始化表格操作类
            helper = new GridHelper<SysDicCategory>(gridViewDic, popupMenu1);

            //2.加载数据
            LoadData();
            ButtonStatus();
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        private void LoadData()
        {
            //权限控制
            if (!allowSelect) return;

            helper.RunCallback(e =>
            {
                e.Result = DicCategoryService.Instance.SelectList();
            })
            .CompletCallback(e =>
            {
                ButtonStatus();
            })
            .RunAsync();
        }

        /// <summary>
        /// 数据编辑
        /// </summary>
        /// <param name="created">是否新建</param>
        /// <param name="model">模型对象</param>
        private void Edit(bool created, SysDicCategory model)
        {
            var editForm = new FrmDicCategoryEdit();
            editForm.SetModel(created, model);
            editForm.ShowDialogOk(LoadData);
        }

        /// <summary>
        /// 获取选中的对象
        /// </summary>
        /// <returns>返回选中的对象</returns>
        private SysDicCategory GetSelected()
        {
            return DicCategoryService.Instance.SelectById(helper.SelectedId);
        }

        /// <summary>
        /// 设置按钮可用状态
        /// </summary>
        private void ButtonStatus()
        {
            btnNewDic.Enabled = allowInsert;
            btnEditDic.Enabled = helper.HasSingleSelected && allowUpdate;
            btnCopyDic.Enabled = helper.HasSingleSelected && allowInsert;
            btnDeleteDic.Enabled = helper.HasSelected && allowDelete;
            btnRefreshCache.Enabled = allowSelect;
        }

        private void FrmDicManager_Load(object sender, EventArgs e)
        {
            InitForm();
        }

        private void gridViewDic_FocusedRowChanged(object sender,
            DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            dicDetailsManagerControl1.SetDicModel(helper.Selected);
            splitContainerControl1.Panel2.Text = helper.Selected.Name;
        }

        private void txtDicName_KeyPress(object sender, KeyPressEventArgs e)
        {
            EnterPressHandle(e, () =>
            {
                var name = txtDicName.Text.Trim();
                gridViewDic.ApplyFindFilter(name);
            });
        }

        private void btnNewDic_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Edit(true, DicCategoryService.Instance.New());
        }

        private void btnEditDic_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Edit(false, GetSelected());
        }

        private void BtnCopy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Edit(true, DicCategoryService.Instance.Copy(GetSelected()));
        }

        private void btnDeleteDic_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            helper.Delete(() =>
            {
                DicCategoryService.Instance.Delete(helper.SelectedId).IfTrue(() => { LoadData(); });
            });
        }

        private void btnReloadDic_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LoadData();
        }

        private void btnConfigDic_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            helper.ShowSettingForm();
        }

        private void BtnRefreshCache_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DicService.Instance.RefreshCache().IfTrue(() =>
            {
                MessageBoxHelper.ShowMessage("服务器字典缓存刷新成功");
            });
        }
    }
}